import { GameObject } from './GameObject'

export class Particle extends GameObject {
    constructor(gameMap, info) {
        //info包含受伤粒子信息, x, y, vx, vy, radius, color, speed, move_length
        super()
        this.gameMap = gameMap
        this.info = info
        this.friction = 0.9
        this.eps = 0.1
    }

    start() {}

    // 根据游戏地图大小重新调整粒子的大小和速度
    resize(widthRatio, heightRatio) {
        try {
            // 确保info对象存在
            if (!this.info) return;
            
            // 按比例缩放位置
            this.info.x *= widthRatio
            this.info.y *= heightRatio

            // 按比例缩放半径和其他属性
            this.info.radius *= heightRatio
            this.info.speed *= heightRatio
            this.info.move_length *= heightRatio
        } catch (e) {
            console.warn('Particle resize error:', e)
        }
    }

    update() {
        try {
            // 安全检查
            if (!this.info || !this.gameMap) {
                this.destroy()
                return false
            }
            
            if (this.info.move_length < this.eps || this.info.speed < this.eps) {
                this.destroy()
                return false
            }
            let moved = Math.min(this.info.move_length, (this.info.speed * this.timedelta) / 1000)
            this.info.x += this.info.vx * moved
            this.info.y += this.info.vy * moved
            this.info.speed *= this.friction
            this.info.move_length -= moved
            this.render()
        } catch (e) {
            console.error('Particle update error:', e)
            this.destroy()
            return false
        }
    }

    render() {
        try {
            if (!this.gameMap || !this.gameMap.ctx || !this.info) return;
            
            this.gameMap.ctx.beginPath()
            this.gameMap.ctx.arc(this.info.x, this.info.y, this.info.radius, 0, Math.PI * 2, false)
            this.gameMap.ctx.fillStyle = this.info.color
            this.gameMap.ctx.fill()
        } catch (e) {
            console.warn('Particle render error:', e)
        }
    }
}
